{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Data download code"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## WRDS configuration"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import wrds\n",
    "wrds_username = \"YOUR_USERNAME\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Daily S&P 500 data (`sp500d.csv`)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Loading library list...\n",
      "Done\n"
     ]
    }
   ],
   "source": [
    "db = wrds.Connection(wrds_username = wrds_username)\n",
    "sp500d = db.get_table(\"crsp\", \"dsp500\", columns=['caldt', 'spindx', 'sprtrn', 'vwretd', 'vwretx'], date_cols=['caldt'], index_col=['caldt'])\n",
    "db.close()\n",
    "sp500d.to_csv(r'sp500d.csv')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Monthly S&P 500 data (`sp500m.csv`)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Loading library list...\n",
      "Done\n"
     ]
    }
   ],
   "source": [
    "db = wrds.Connection(wrds_username = wrds_username)\n",
    "sp500m = db.get_table(\"crsp\", \"msp500\", columns=['caldt', 'spindx', 'sprtrn', 'vwretd', 'vwretx'], date_cols=['caldt'], index_col=['caldt'])\n",
    "db.close()\n",
    "sp500m.to_csv(r'sp500m.csv')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.9.13 ('fdap')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "758146b731c3fd8b39ea7cab27c121b86244c5bbc1f629f2f080e03f5074f425"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
